package com.spring.boot.rpc.rpc07;

import com.spring.boot.rpc.rpc00.IProductServerImpl;
import com.spring.boot.rpc.rpc00.IUserService;
import com.spring.boot.rpc.rpc00.User;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 */
public class Server {
    private static boolean running = true;

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(8888);
        while (running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();


    }

    private static void process(Socket s) throws IOException, Exception {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        ObjectInputStream ois = new ObjectInputStream(in);

        // 按客户端传入的顺序读取
        String clazzName = ois.readUTF();// 读取类名
        String methodName = ois.readUTF();// 读取方法名
        Class[] parameterTypes = (Class[]) ois.readObject();// 读取参数类型
        Object[] args = (Object[])ois.readObject();// 读取参数

        Class clazz = null;
        clazz = Class.forName(clazzName);
        Method method = clazz.getMethod(methodName,parameterTypes);
        Object o = method.invoke(clazz.newInstance(), args);

        /**
         * 改成把对象往外写
         */
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(o);
        oos.flush();

    }




}
